import { getWeblogs } from "../../service/api_weblog"

const weblogModule = {
  namespaced: true,
  state: () => {
    return {
      weblogs: [],
      // 进行模糊查询的weblogs
      filterWeblogs: [],
      weblogBackground: "#eee",
      weblogColor: "#666",
      status: true,
      // 文章是否发布成功
      flag: false
    }
  },
  getters: {
    // 模糊查询
    filterWeblogs(state) {
      return keywords => {
        return keywords ? state.weblogs.filter(item => item.title.includes(keywords)) : state.weblogs
      }
    }
  },
  mutations: {
    getWeblogsMutation(state, payload) {
      const { data } = payload.result
      state.weblogs = data
      state.filterWeblogs = data
    },
    // 改变背景颜色
    changeWeblogBgcMutation(state, playload) {
      state.status = playload.status
      state.weblogBackground = playload.status ? "#eee" : "#666"
      state.weblogColor = playload.status ? "#666" : "#eee"
    },
    // 改变文章是否发布成功标志
    changeWeblogFlagMutation(state) {
      state.flag = !state.flag
    },
    filterWeblogs(state, keywords) {
      state.filterWeblogs = this.getters["weblog/filterWeblogs"](keywords)
    }
  },
  actions: {
    // 获取全部文章
    async getWeblogsAction({ commit }) {
      const result = await getWeblogs()
      commit("getWeblogsMutation", { result })
    }
  }
}

export default weblogModule